/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "secure_element_channel.h"
#include "secure_element_common.h"

namespace OHOS {
namespace SE {
namespace OMAPI {
void SEChannelImpl::SetChannel(std::shared_ptr<Channel> channel)
{
    channel_ = channel;
}

void SEChannelImpl::SetSession(SESessionImpl *se)
{
    session_ = se;
}

int64_t SEChannelImpl::GetSession(int32_t *errorCode)
{
    if (session_ == nullptr || channel_ == nullptr) {
        ErrorLog("SEChannelImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return CJ_INVALID_VALUE_64;
    }
    return session_->GetID();
}

void SEChannelImpl::Close(int32_t *errorCode)
{
    if (channel_ == nullptr) {
        ErrorLog("SEChannelImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return;
    }
    int32_t statusCode = channel_->Close();
    if (statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEChannelImpl: Close failed");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
    }
}

bool SEChannelImpl::IsClosed(int32_t *errorCode)
{
    bool isClose = false;
    if (channel_ == nullptr) {
        ErrorLog("SEChannelImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return isClose;
    }
    int32_t statusCode = channel_->IsClosed(isClose);
    if (statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEChannelImpl: IsClosed failed");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
        return false;
    }
    return isClose;
}

bool SEChannelImpl::IsBasicChannel(int32_t *errorCode)
{
    bool isBasicChannel = false;
    if (channel_ == nullptr) {
        ErrorLog("SEChannelImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return isBasicChannel;
    }
    int32_t statusCode = channel_->IsBasicChannel(isBasicChannel);
    if (statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEChannelImpl: IsBasicChannel failed");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
        return false;
    }
    return isBasicChannel;
}

std::string SEChannelImpl::GetSelectResponse(int32_t *errorCode)
{
    std::string str = "";
    if (channel_ == nullptr) {
        ErrorLog("SEChannelImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return str;
    }
    int32_t statusCode = channel_->GetSelectResponse(str);
    if (statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEChannelImpl: GetSelectResponse failed");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
        return "";
    }
    return str;
}

std::string SEChannelImpl::Transmit(const std::string &command, int32_t *errorCode)
{
    std::string str = "";
    if (channel_ == nullptr) {
        ErrorLog("SEChannelImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return str;
    }
    int32_t statusCode = channel_->Transmit(command, str);
    if (statusCode != ErrorCode::SE_SUCCESS || str.empty()) {
        ErrorLog("SEChannelImpl: Transmit failed");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
        return "";
    }
    return str;
}
}  // namespace OMAPI
}  // namespace SE
}  // namespace OHOS